﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.ServerData
{
    /// <summary>
    /// 成交明细
    /// </summary>
    public class ServiceT_TradeDetail_Summary : Benefit.Interface.ServerData.IT_TradeDetail_Summary
    {
        Benefit.DB.DBManager db = null;
        public ServiceT_TradeDetail_Summary(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 查询结算后的持仓明细情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_TradeDetail_Summary> GetInfo(string pdate)
        {
            List<Models.ServerData.T_TradeDetail_Summary> tth = new List<Models.ServerData.T_TradeDetail_Summary>();
            Interface.ServerData.IT_Settle_History tsh = new ServerData.ServiceT_Settle_History(db);
            // T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                tth = GetTradeSummaryHistory(settlementid, pdate);
            }
            return tth;
        }

        /// <summary>
        /// 查询某天最大结算编号的成交汇总
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        public System.Collections.Generic.List<Models.ServerData.T_TradeDetail_Summary> GetTradeSummaryHistory(int settlementid, string pdate)
        {
            DavidDB.ADO.DBManager dbmanager = new DavidDB.ADO.DBManager();
            System.Collections.Generic.List<Models.ServerData.T_TradeDetail_Summary> tth = new System.Collections.Generic.List<Models.ServerData.T_TradeDetail_Summary>();
            string sql = "select * from T_TradeDetail_Summary where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            dynamic sdr = dbmanager.ExecuteReader(sql);
            while (sdr.Read())
            {
                Models.ServerData.T_TradeDetail_Summary s = new Models.ServerData.T_TradeDetail_Summary();
                s.AccountId = Convert.ToInt32(sdr["accountid"]);
                s.TradingDay = sdr["TradingDay"].ToString();
                s.Brokerid = Convert.ToString(sdr["Brokerid"]);
                s.CloseProfit = Convert.ToDouble(sdr["CloseProfit"]);
                s.CloseProfit2 = Convert.ToDouble(sdr["CloseProfit2"]);
                s.Commission = Convert.ToDouble(sdr["Commission"]);
                s.TradePrice = Convert.ToDouble(sdr["TradePrice"]);
                s.Direction = Convert.ToString(sdr["Direction"]);
                s.HedgeFlag = Convert.ToString(sdr["HedgeFlag"]);
                s.Id = Convert.ToInt32(sdr["Id"]);
                s.InstrumentId = Convert.ToString(sdr["InstrumentId"]);
                s.Investorid = Convert.ToString(sdr["Investorid"]);
                s.OffsetFlag = Convert.ToString(sdr["OffsetFlag"]);
                s.SettlementId = Convert.ToInt32(sdr["SettleMentId"]);
                s.TotalCost = Convert.ToDouble(sdr["TotalCost"]);
                s.Volume = Convert.ToInt32(sdr["Volume"]);
                tth.Add(s);
            }
            sdr.Close();
            dbmanager.Commit();
            dbmanager.Dispose();
            return tth;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerTradeSummaryDataIntoBenefit(string pdate)
        {
            Interface.ServerData.IT_TradeDetail_Summary h = new ServerData.ServiceT_TradeDetail_Summary(db);
            //Models.ServerData.T_TradeDetail_Summary h = new Models.ServerData.T_TradeDetail_Summary();
            List<Models.ServerData.T_TradeDetail_Summary> tth = h.GetInfo(pdate);
            foreach (Models.ServerData.T_TradeDetail_Summary ha in tth)
            {
                db.TradeDetailSummary.Add(ha);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 删除某日的所有持仓明细记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDayTradeSummaryFromBenefit(string pdate)
        {
            var historys = from t in db.TradeDetailHistory where t.TradingDay.Equals(pdate) select t;
            foreach (Models.ServerData.T_TradeDetail_History ha in historys)
            {
                db.TradeDetailHistory.Remove(ha);
            }
            db.SaveChanges();
        }
    }
}